package com.gu.myviewmodel.ui.homepage.ui

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.gu.myviewmodel.databinding.ListItemBinding
import com.gu.myviewmodel.services.Car

class CarAdapter : Adapter<ViewHolder>() {
    private var data = mutableListOf<Car>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(ListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(data[position])
    }

    override fun getItemCount(): Int {
        return data.size
    }

    fun bingData(list: List<Car>) {
        if (data.isNotEmpty())
            data.clear()
        data.addAll(list)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun refresh() {
        notifyDataSetChanged()
    }

    fun clear() {
        data.clear()
    }
}

class ViewHolder constructor(private val binding: ListItemBinding) : RecyclerView.ViewHolder(binding.root) {
    fun bind(car: Car) {
        binding.car = car
        binding.executePendingBindings()
    }
}